package ch08_Swing高级组件.tree;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class HardwareTreeCellRenderer extends DefaultTreeCellRenderer{

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf,
			int row, boolean hasFocus) {
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
		Hardware hw = (Hardware) node.getUserObject();
		String id = hw.getId();
		String name = hw.getName();
		JLabel nodeLab  = new JLabel();
		nodeLab.setOpaque(true);
		
		nodeLab.setText(id.substring(id.length() -2)+"-"+name);
		if(hw.getIcon()!=null) {
			nodeLab.setIcon(hw.getIcon());
		}
		if(selected) {
			nodeLab.setBackground(Color.LIGHT_GRAY);
		}else {
			nodeLab.setBackground(Color.WHITE);
		}
		return nodeLab;
	}
	
}
